package com.ouc.dataMange.service.impl;

import com.ouc.common.constant.VariableName;
import com.ouc.common.entity.NcData;
import com.ouc.common.ncRead.NcDao;
import com.ouc.common.utils.Arith;
import com.ouc.common.utils.DateUtils;
import com.ouc.common.utils.StringUtils;
import com.ouc.common.utils.file.FileUploadUtils;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.dataMange.domain.Upload;
import com.ouc.dataMange.service.NcUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.File;
import java.util.*;

import static com.ouc.common.constant.NcConstants.*;


/**
 * Nc上传文件实现类
 *
 * @author B504
 */
@Service
public class NcUploadServiceImpl implements NcUploadService {
  @Autowired
  NcDao ncDao;

  @Override
  public Upload getNcUploadFile(String filePath) {
    try {
      Upload upload = new Upload();
      try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
        List<Variable> variableExcludeList = ncDao.getVariableExclude(ncFile);
        Map<String, NcData> ncDataOfOneDimensional = ncDao.getNcDataOfOneDimensional(ncFile);
        float[] lon = ncDataOfOneDimensional.get(ncDao.getVariable(ncFile, VariableName.LON).getShortName()).getData1D();
        float[] lat = ncDataOfOneDimensional.get(ncDao.getVariable(ncFile, VariableName.LAT).getShortName()).getData1D();
        String variableName;
        float[] level = null;
        float lonInterval = (float) Arith.round(Arith.sub(lon[1], lon[0]), 4);
        float latInterval = (float) Arith.round(Arith.sub(lat[1], lat[0]), 4);
        float startLon = lon[0];
        float endLon = lon[lon.length - 1];
        float startLat = lat[0];
        float endLat = lat[lat.length - 1];
   /* //  获取时间
    String ncTime = ncDao.getNcTime(ncFile);*/
//      Variable variable1 = ncDao.getVariable(ncFile, VariableName.LEVEL);
//      NcData ncData = ncDataOfOneDimensional.get(ncDao.getVariable(ncFile, VariableName.LEVEL).getShortName());
        if (ncDao.getVariable(ncFile, VariableName.LEVEL) != null && ncDataOfOneDimensional.get(ncDao.getVariable(ncFile, VariableName.LEVEL).getShortName()).getData1D() != null) {
          level = ncDataOfOneDimensional.get(ncDao.getVariable(ncFile, VariableName.LEVEL).getShortName()).getData1D();
        }
        // 对变量名进行重新命名
        List<String> variableList = new ArrayList<>();
        for (Variable variable : variableExcludeList) {
          variableList.add(ncDao.variableRename(variable.getShortName()));
        }
        if (variableList.size() > 1) {
          variableName = MULTY;
        } else {
          variableName = variableList.get(0);
        }
        List<String> currentList = Arrays.asList(VariableName.UFLOW.getVariableName()[0], VariableName.VFLOW.getVariableName()[0]);
        List<String> windList = Arrays.asList(VariableName.WINDU.getVariableName()[0], VariableName.WINDV.getVariableName()[0]);
        List<String> waveList = Arrays.asList(VariableName.WAVEU.getVariableName()[0], VariableName.WAVEV.getVariableName()[0]);

        if (variableList.containsAll(currentList)) {
          variableList.add(CURRENTUV);
        }
        if (variableList.containsAll(windList)) {
          variableList.add(WINDUV);
        }
        if (variableList.containsAll(waveList)) {
          variableList.add(WAVEUV);
        }
        upload.setFileId(null);
        String fileTime = StringUtils.getContinuityNum(FileUtils.getName(filePath), 8);
        if (fileTime.length() == 0) {
          fileTime = ncDao.getNcTime(ncFile);
          upload.setFileTime(DateUtils.dateTime("yyyy-MM-dd", fileTime));
        } else {
          upload.setFileTime(DateUtils.dateTime("yyyyMMdd", fileTime));
        }
        upload.setVariableName(variableName);
        upload.setVariableList(variableList.toString());
        upload.setStartLon((double) startLon);
        upload.setEndLon((double) endLon);
        upload.setStartLat((double) startLat);
        upload.setEndLat((double) endLat);
        upload.setLonInterval((double) lonInterval);
        upload.setLatInterval((double) latInterval);
        upload.setLevelNum(level == null ? null : level.length);
        upload.setFilePath(filePath);
        return upload;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

  }

  @Override
  public int ncFileUpload(MultipartFile file, String destFilePath) {
      /*之所以并不全部使用File.separator,是因为windows会识别错误
      若依的路径都是/，但是windows路径都是
      *
      */
    if (!new File(destFilePath).exists()) {
      new File(destFilePath).mkdirs();
    }
    File[] files = new File(destFilePath).listFiles();
    boolean flag = true;
    if (files != null && files.length > 0) {
      for (File s : files) {
        if (Objects.equals(file.getOriginalFilename(), s.getName())) {
          flag = false;
        }
      }
      if (flag) {
        try {
          FileUploadUtils.upload(destFilePath, file);
          return 1;
        } catch (Exception e) {
          return 0;
        }
      } else {
        return 0;
      }
    } else {
      try {
        FileUploadUtils.upload(destFilePath, file);
        return 1;
      } catch (Exception e) {
        e.printStackTrace();
        return 0;
      }
    }
  }

}
